<template>
	<view>
		<uni-nav-bar left-icon="back" left-text="返回" fixed="true" statusBar="true"  @clickLeft="leftAction" :title='baseStationId+"注册终端"'>
		</uni-nav-bar>
		<view class="content">
			<form @submit="formSubmit">
				<view class="uni-form-item uni-row">
					<view class="title">终端ID号(TI)</view>
					<input class="uni-input" name="ti" v-model="ti" placeholder="终端ID号" />
				</view>

				<view class="uni-form-item uni-row">
					<view class="title">数据通道号(DNUM)</view>
					<view class="uni-input">
						<radio-group name="dnum" >
							<label class="radio">
								<radio value="D1" checked="true" />D1
							</label>
							<label class="radio">
								<radio value="D2" />D2
							</label>
						</radio-group>
					</view>
				</view>

				<view class="uni-btn-v">
					<button form-type="submit">新增</button>
				</view>
			</form>
		</view>
		<view v-if="dataList.length>0">
			<uni-collapse ref="add" class="warp" @change="change">
				<uni-collapse-item v-for="(sub, key) in dataList" :key="key" :open="sub.open" :title="sub.name">
					<uni-list>
						<template v-for="(item,index) in sub.dataArr">
							<uni-list-item :note="item.subname" :rightText="item.value" />
						</template>
					</uni-list>
				</uni-collapse-item>
			</uni-collapse>
		</view>
	</view>
</template>

<script>
	import uniNavBar from '@/uni_modules/uni-nav-bar/components/uni-nav-bar/uni-nav-bar.vue'
	import wsconnect from '@/api/webservice.js'

	export default {
		data() {
			return {
				dataList: [],
				ti: '',
			};
		},
		onLoad(options) {
			this.sendCmdTagName = options.s
			this.reqTagName = options.r
			this.baseStationId = options.id
		},
		components: {
			uniNavBar
		},
		methods: {
			leftAction() {
				uni.navigateBack({
					delta: 1
				});
			},
			formSubmit: function(e) {
				const sendCmdTagName = this.sendCmdTagName
				const reqTagName = this.reqTagName
				var formdata = e.detail.value
				const sendCmd = 'TerRegRq,/TI=' + formdata.ti + '/DNUM=' + formdata.dnum + '/'
				wsconnect.requestJson({
					items: [sendCmdTagName + '.DESC'],
					vals: [sendCmd],
					type: "SetDataByName",
				}, reqTagName).then(res => {
					this.ti = ''
					this.configDataList(res)
					wsconnect.clearReqData(1, reqTagName)
				}).catch(error => {
					if (error.code == 1) {
						uni.redirectTo({
							url: "../index/index"
						})
					} else {
						uni.showModal({
							content: error.msg,
							showCancel: false
						})
					}
				})
			},
			configDataList(res) {
				const arr = res.split('/')
				const array = arr.slice(1, arr.length - 1)
				var dataDic = {}
				for (let i in array) {
					const result = array[i]
					const a = result.split('=')
					dataDic[a[0]] = a[1]
				}
				
				
				//发射功率
				const tp = item['TP']
				const tp_arr = ["0dBm", "2dBm", "5dBm", "7dBm", "10dBm", "14dBm", "17dBm", "22dBm"]
				const tp_str = tp_arr[tp - 1]
				
				//空中唤醒
				const dc = item['DC']
				const dc_arr = ["休眠2s/接收30ms", "休眠2s/接收40ms", "休眠3s/接收40ms", "休眠4s/接收40ms", "休眠5s/接收40ms"]
				const dc_str = dc_arr[dc - 1]
				
				//采样间隔
				const sp = item['SP']
				const sp_arr = ['1s', '5s', '10s', '30s', '1min', '3min', '5min', '10min', '15min', '30min', '1h']
				const sp_str = sp_arr[sp - 1]
				
				//上传间隔
				const ut = item['UT']
				const ut_arr = ['1s', '5s', '30s', '1min', '3min', '5min', '10min', '15min', '30min', '1h', '2h']
				const ut_str = ut_arr[ut - 1]
				
				const model = {
					name: "基站名:" + item['TI'],
					dataArr: [{
							subname: "终端ID",
							value: item['TI'],
							key: 'TI'
						},
						{
							subname: "类型",
							value: item['TY'] == 1 ? 'PT100' : '压力计',
							key: 'TY'
						},
						{
							subname: "配置通道",
							value: item['CH'],
							key: 'CH'
						},
						{
							subname: "数据通道",
							value: item['DH'],
							key: 'DH'
						},
						{
							subname: "报警通道",
							value: item['AH'],
							key: 'AH'
						},
						{
							subname: "发射功率",
							value: tp_str,
							key: 'TP'
						},
						{
							subname: "空中唤醒",
							value: dc_str,
							key: 'DC'
						},
						{
							subname: "采样间隔",
							value: sp_str,
							key: 'SP'
						},
						{
							subname: "上传间隔",
							value: ut_str,
							key: 'UT'
						},
						{
							subname: "报警上限",
							value: item['HT'],
							key: 'HT'
						},
						{
							subname: "报警下限",
							value: item['LT'],
							key: 'LT'
						},
						{
							subname: "校准",
							value: item['CV'],
							key: 'CV'
						},
						{
							subname: "电量",
							value: item['BV'],
							key: 'BV'
						},
						{
							subname: "RSSI",
							value: item['RS'],
							key: 'RS'
						},
						{
							subname: "SN",
							value: item['SN'],
							key: 'SN'
						},
					]
				
				}
				
				this.dataList.push(model)
			}
		}
	}
</script>

<style lang="scss">
	@import '@/common/uni.css';

	.uni-form-item .title {
		margin-left: 20rpx;
		padding: 20rpx 0;
		width: 300rpx;
		font-size: 30rpx;
	}

	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.uni-btn-v {
		margin-left: 50rpx;
		margin-right: 50rpx;
		height: 40rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}

	.uni-input {
		background-color: #F1F1F1;
		height: 60rpx;
		border: 1px solid #DDD;
		margin-right: 50rpx;
	}
</style>
